package com.aplana.ccsd.web.component;

import info.magnolia.module.blossom.annotation.TabFactory;
import info.magnolia.module.blossom.annotation.Template;
import info.magnolia.module.blossom.annotation.TemplateDescription;
import info.magnolia.module.blossom.dialog.TabBuilder;

import javax.jcr.Node;
import javax.jcr.PathNotFoundException;
import javax.jcr.RepositoryException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.annotation.RequestMapping;

import com.aplana.ccsd.model.ProjectOverview;
import com.aplana.ccsd.service.ProjectService;

/**
 * Компонент обзор случайного проекта
 */
@Controller
@Template(title = "Random project overview", id = "blossomCCSD:components/randomProjectOverview")
@TemplateDescription("Обзор случайного проекта")
public class RandomProjectOverviewComponent {

	@Autowired
	private ProjectService projectService;

	@RequestMapping("/randomProjectOverview")
	public String handleRequest(ModelMap model, HttpSession session,
			HttpServletRequest request, Node node)
			throws PathNotFoundException, RepositoryException,
			ServletRequestBindingException {
		
		ProjectOverview result = projectService.getRandomProjectOverview(node);	
		model.put("projectOverview", result);
		
		return "components/projectOverview.jsp";
	}

	@TabFactory("Content")
	public void contentTab(TabBuilder tab) {
		projectService.addRandomProjectOverviewDialogControls(tab);
	}
}
